import { http, domainBase } from "../../utils/http";
Page({
  data: {
    detail: {},
  },
  async onLoad(e) {
    let { id } = e;
    let { data: res } = await http({ url: "getgoodsinfo", data: { id } }).catch(
      (err) => err
    );
    let detail = res.list[0];
    detail.img = domainBase + detail.img;
    this.setData({
      detail: detail,
    });
  },
  //添加收藏    ["联想小新","华为MateBook13"，...]
  addLike(e) {
    //新来的
    let { goodsname } = e.currentTarget.dataset;
    //以前的数组
    let arr = wx.getStorageSync("arr") || [];
    //检查商品名称在缓存中是否存在
    let index = arr.findIndex((item) => {
      return item == goodsname;
    });
    //如果不存在，则添加到缓存
    if (index == -1) {
      arr.push(goodsname);
      wx.setStorageSync("arr", arr);
    }
  },
  //添加购物车
  addCart() {
    let cart = wx.getStorageSync("cart") || [];
    this.data.detail.img = this.data.detail.img;
    this.data.detail.checked = false;
    this.data.detail.count = 1;
    cart.unshift(this.data.detail);
    wx.setStorageSync("cart", cart);
    wx.showToast({
      title: "成功添加购物车",
    });
  },
});
